/**
 * Created by Administrator on 2017/6/27.
 */
define(['swiper', 'jquery', 'vue',  'ks-spa','async!BMap'], function (swiper, $, Vue,spa) {
    var currentPage = 1;
    var keyword = "";
    var order = "id";
    var sort = "desc";
    var city_searchValue = "";

    var sw = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });

    // var _self;
    mui.init({
        pullRefresh: {
            container: '#refreshContainer',
            down: {
                height: 50,//可选,默认50.触发下拉刷新拖动距离,
                auto: false,//可选,默认false.自动下拉刷新一次
                contentdown: "正在刷新...",//可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
                contentover: "正在刷新...",//可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
                contentrefresh: "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
                callback: pulldownRefresh//必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
            },
            up: {
                contentrefresh: '正在加载...',
                contentnomore: '没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
                callback: pullupRefresh
            }
        }
    });
    var vue = new Vue({
        el: '#data_ul',
        data: {
            items: []
        },
        methods: {
            detailbtn: function (id, distance) {
                window.location.href = goods_url + "/store_id/" + id + "?distance=" + distance;
            }
        }
    });

    function getData(flag) {
        var data = {
            'order': order,
            "sort": sort,
            'city_searchValue': city_searchValue,
            "keyword": keyword,
            'page': currentPage
        };
        $.ajax({
            type : "get",
            url : list_url,
            data : data,
          //  async : false,
            beforeSend: function (XMLHttpRequest) {
                spa.showLoading();
            },
            success: function (result) {
                spa.hideLoading();
                if (result.code === 1) {
                    switch (flag) {
                        case "up":
                            if (result.data.length > 0) {
                                vue.items.push(result.data);
                                currentPage++;
                                mui('#refreshContainer').pullRefresh().endPullupToRefresh();
                            }
                            else {
                                mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                            }
                            break;
                        case "down":
                            mui('#refreshContainer').pullRefresh().endPulldownToRefresh();
                            break;
                    }
                }
            },
            complete: function (XMLHttpRequest, textStatus) {
                spa.hideLoading();
            }
        });
    }

    // <editor-fold defaultstate="collapsed" desc="关键字查询">
    function searchKey(key) {
        currentPage = 1;
        keyword = key;
        vue.items.splice(0, vue.items.length);
        getData("up");
    }
    getData("up");

    // if(lat==="0"&&lng==="0"){
    //     autoPosition();
    // }
    //
    // function autoPosition() {
    //     var geolocation = new BMap.Geolocation();
    //     geolocation.getCurrentPosition(function (r) {
    //
    //         if (this.getStatus() == BMAP_STATUS_SUCCESS) {
    //             // var mk = new BMap.Marker(r.point);
    //             // map.addOverlay(mk);
    //             console.log(r);
    //             console.log('您的位置：' + r.point.lng + ',' + r.point.lat);
    //
    //             lng = r.point.lng;
    //             lat = r.point.lat;
    //             $.post(save_point_url,{lng:lng,lat:lat},function (result) {
    //
    //             });
    //             for (var is in  vue.items) {
    //                 for (var i in  vue.items[is]){
    //                     var distance = getGreatCircleDistance(lat, lng, vue.items[is][i].latitude, vue.items[is][i].longitude) / 1000;
    //                     vue.items[is][i].distance = distance.toFixed(2);
    //                 }
    //             }
    //
    //         }
    //         else {
    //             alert('failed' + this.getStatus());
    //         }
    //     }, {enableHighAccuracy: true})
    // }

    document.getElementById('word').addEventListener('change', function (o) {
        keyword = this.value;
        searchKey(keyword);
    });
    // </editor-fold>

    /**
     * 上拉加载具体业务实现
     */
    function pullupRefresh() {
        getData("up");
    }

    /**
     * 下拉刷新具体业务实现
     */
    function pulldownRefresh() {
        currentPage = 1;
        getData("down");
    }
});